#include "ScriptPCH.h"
#include "Player.h"
 
class npc_start_option : public CreatureScript
{
    public:
 
        npc_start_option()
            : CreatureScript("npc_start_option")
        {
        }
		
		void HomeBind(Player* pPlayer, int mapid, float positionX, float positionY, float positionZ, int zone)
		{
		    CharacterDatabase.PExecute("UPDATE character_homebind SET map = %d , position_x = '%f', position_y = '%f', position_z = '%f', zone = %d WHERE guid = %u",mapid, positionX, positionY, positionZ, zone, pPlayer->GetGUIDLow());
				
			pPlayer->m_homebindMapId = mapid;
    		pPlayer->m_homebindAreaId = zone;
    		pPlayer->m_homebindX = positionX;
    		pPlayer->m_homebindY = positionY;
    		pPlayer->m_homebindZ = positionZ;
		}
 
        void Options(Player* pPlayer, Creature* pCreature, int option, uint32 spec, uint32 sspec)
        {
     
            if (option == 1)
            {
              QueryResult result_items = WorldDatabase.PQuery("SELECT entry FROM item_instant85 WHERE class = %u AND spec = %u AND sspec = %u", pPlayer->getClass(), spec, sspec);
              QueryResult result_accid = CharacterDatabase.PQuery("SELECT account FROM characters WHERE guid = '%u'", pPlayer->GetGUIDLow());
			  Field *field = result_accid->Fetch();
              uint32 accid = field[0].GetUInt32();
			  QueryResult result_money = LoginDatabase.PQuery("SELECT money_mod FROM account_start_gold WHERE accountid = '%u'", accid);
			  QueryResult result_spells = WorldDatabase.PQuery("SELECT spellid FROM spell_instant85 WHERE class = %u", pPlayer->getClass());
			  uint32 itemEntry;
              uint32 money = 5000000;  // 500 Gold
              uint32 money_mod = 0;
			  uint32 spellEntry;
                do
                {
                  Field *field = result_items->Fetch();
                  itemEntry = field[0].GetUInt32();
                  pPlayer->AddItem(itemEntry, 1);
                }while(result_items->NextRow());
    
              if(!result_money)
              {
                LoginDatabase.PExecute("INSERT INTO account_start_gold VALUES (%u, 4)",accid);
                money_mod = 5;
              } 
              else
              {
                Field *field = result_money->Fetch();
                money_mod = field[0].GetUInt32();
                if(money_mod > 0)
                LoginDatabase.PExecute("UPDATE account_start_gold SET money_mod = money_mod - 1 WHERE accountid = %u",accid);
              }
             
                pPlayer->SetLevel(85);
                if(money_mod > 0)
                  money = money*money_mod;
                else
                  money = 0;
				  
                pPlayer->SetMoney(money);
                pPlayer->learnSpell(90267,false);  //Flying in Azeroth/Deepholm
                pPlayer->learnSpell(34091,false);
             
                if(pPlayer->GetTeam() == ALLIANCE) //Alliance
                   pPlayer->learnSpell(61229,false);  //Alliance Mount
                else  //Horde
                   pPlayer->learnSpell(32244,false);  //Horde Mount
               
               

                do
                {
                  Field *fielda = result_spells->Fetch();
                  spellEntry = fielda[0].GetUInt32();
                  pPlayer->learnSpell(spellEntry,false);
                }while(result_spells->NextRow());
             
                if(pPlayer->getClass() == 6)
                {
                    pPlayer->SetFreeTalentPoints(41);
                    pPlayer->SendTalentsInfoData(true);
                    pPlayer->learnSpell(50977,false);  //Dark Portal
                    pPlayer->learnSpell(53428,false);    //Rune Smith
                    pPlayer->learnSpell(48778,false);    //DK Mount 
                }
             
                if(pPlayer->getRace() == 22)  //Worgen
                {
                    pPlayer->learnSpell(68996,false);  //Two Forms
                    pPlayer->learnSpell(87840,false);  //Running Wild
                }
             
                if(pPlayer->getRace() == 9)  //Goblin
                    pPlayer->learnSpell(69046,false);  //Packhobgoblin
             
                if(pPlayer->GetTeam() == ALLIANCE) //Alliance
				{
                  pPlayer->TeleportTo(0, -8830.7f, 624.16f, 95.1f, 0.0f, 0);
				  HomeBind(pPlayer, 0, -8830.7f, 624.16f, 95.1f, 1519);
				}
                else //Horde
				{
                  pPlayer->TeleportTo(1, 1572.7f, -4398.16f, 16.1f, 0.0f, 0);
				  HomeBind(pPlayer, 1, 1572.7f, -4398.16f, 16.1f, 1637);
				}
 
            }
         
            else if (option == 2)
            {
                uint32 player_race = pPlayer->getRace();
                switch(player_race)
                {
                    case 1:  //Human
                    pPlayer->TeleportTo(0, -8914.7f, -134.16f, 80.6f, 5.2f, 0);
					HomeBind(pPlayer, 0, -8914.7f, -134.16f, 80.6f, 12);
                    break;
                 
                    case 2:  //Orc
                    pPlayer->TeleportTo(1, -618.7f, -4251.16f, 38.8f, 0.0f, 0);
					HomeBind(pPlayer, 1, -618.7f, -4251.16f, 38.8f, 14);
                    break;
                 
                    case 3:  //Dwarf
                    pPlayer->TeleportTo(0, -6230.4f, 330.16f, 383.8f, 6.2f, 0);
					HomeBind(pPlayer, 0, -6230.4f, 330.16f, 383.8f, 1);
                    break;
                 
                    case 4:  //Night Elf
                    pPlayer->TeleportTo(1, 10311.2f, 831.16f, 1326.4f, 5.69f, 0);
					HomeBind(pPlayer, 1, 10311.2f, 831.16f, 1326.4f, 141);
                    break;
                 
                    case 5:  //Undead
                    pPlayer->TeleportTo(0, 1698.9f, 1703.302f, 135.8f, 5.13f, 0);
					HomeBind(pPlayer, 0, 1698.9f, 1703.302f, 135.8f, 85);
                    break;
                 
                    case 6:  //Tauren
                    pPlayer->TeleportTo(1, -2909.9f, -256.52f, 59.8f, 0.38f, 0);
					HomeBind(pPlayer, 1, -2909.9f, -256.52f, 59.8f, 215);
                    break;
                 
                    case 7:  //Gnome
                    pPlayer->TeleportTo(0, -4982.91f, 876.52f, 274.4f, 3.28f, 0);
					HomeBind(pPlayer, 0, -4982.91f, 876.52f, 274.4f, 1);
                    break;
                 
                    case 8:  //Troll
                    pPlayer->TeleportTo(1, -1171.44f, -5263.6499f, 0.848f, 5.933f, 0);
					HomeBind(pPlayer, 1, -1171.44f, -5263.6499f, 0.848f, 14);
                    break;
                 
                    case 10:  //Blood Elf
                    pPlayer->TeleportTo(530, 10349.599f, -6357.29f, 33.57f, 5.31f, 0);
					HomeBind(pPlayer, 530, 10349.599f, -6357.29f, 33.57f, 3430);
                    break;
                 
                    case 11:  //Draenei
                    pPlayer->TeleportTo(530, -3961.639f, -13931.2f, 100.6134f, 2.08f, 0);
					HomeBind(pPlayer, 530, -3961.639f, -13931.2f, 100.6134f, 3524);
                    break;
                 
                    case 9:  //Goblin
                    pPlayer->TeleportTo(648, -8423.839f, 1365.88f, 104.6774f, 4.68f, 0);
					HomeBind(pPlayer, 648, -8423.839f, 1365.88f, 104.6774f, 4737);
                    break;
                 
                    case 22:  //Worgen
                    pPlayer->TeleportTo(638, -1443.619f, 1409.8f, 35.6134f, 3.18f, 0);
					HomeBind(pPlayer, 638, -1443.619f, 1409.8f, 35.6134f, 4755);
                    break;
                }
            }
         
         
         
            else if (option == 3)
            {
              pPlayer->AddItem(38674, 1);
              pPlayer->AddItem(38667, 1);
              pPlayer->AddItem(38663, 1);
              pPlayer->AddItem(38662, 1);
              pPlayer->AddItem(40483, 1);
              pPlayer->AddItem(39322, 1);
              pPlayer->AddItem(39208, 1);
              pPlayer->AddItem(38671, 1);
              pPlayer->AddItem(38666, 1);
              pPlayer->AddItem(38669, 1);
              pPlayer->AddItem(38675, 1);
              pPlayer->AddItem(38670, 1);
              pPlayer->AddItem(38672, 1);
              pPlayer->AddItem(38632, 1);
              pPlayer->AddItem(38665, 1);
              pPlayer->AddItem(38661, 1);
              pPlayer->SetFreeTalentPoints(16);
              pPlayer->SendTalentsInfoData(true);
              pPlayer->learnSpell(50977,false);  //Dark Portal
              pPlayer->learnSpell(53428,false);    //Rune Smith
              pPlayer->learnSpell(48778,false);    //DK Mount 
              pPlayer->TeleportTo(530, -246.695f, 962.65f, 84.34f, 1.6f, 0);
              HomeBind(pPlayer, 530, -246.695f, 962.65f, 84.34f, 3483);
              pPlayer->SetMoney(200000);
           
           
              uint32 player_race = pPlayer->getRace();
             
              pPlayer->learnSpell(90267,false);  //Flying in Azeroth/Deepholm
              pPlayer->learnSpell(34091,false);
             
              if(player_race == 1  ||
                player_race == 3  ||
                player_race == 4  ||
                player_race == 7  ||
                player_race == 11 ||
                player_race == 22  ) //Alliance
               
                pPlayer->learnSpell(61229,false);  //Alliance Mount
             
                else  //Horde
                pPlayer->learnSpell(32244,false);  //Horde Mount
            }
            pPlayer->PlayerTalkClass->ClearMenus();
            OnGossipHello(pPlayer, pCreature);
        }
 
        bool OnGossipHello(Player* pPlayer, Creature* pCreature)
        {
          pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I AGREE", GOSSIP_SENDER_MAIN, 600);
          pPlayer->PlayerTalkClass->SendGossipMenu(719, pCreature->GetGUID());
		  
            return true;
        }
 
        bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 , uint32 uiAction)
        {
            pPlayer->PlayerTalkClass->ClearMenus();
         
            switch (uiAction)
            {
			
			case 600:
			    pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start on Level 85!", GOSSIP_SENDER_MAIN, 1000);
				if(pPlayer->getClass() != 6)
                  pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start on Level 1!", GOSSIP_SENDER_MAIN, 1001);
                else
               {
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to skip the DK Start Area!", GOSSIP_SENDER_MAIN, 10);
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to play the DK Start Area !IMPORTANT! Bugs are possible!", GOSSIP_SENDER_MAIN, 11);
               }
 
            pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());
			break;
			
            case 1000:
                if(pPlayer->getClass() == 1 || pPlayer->getClass() == 2 || pPlayer->getClass() == 6 || pPlayer->getClass() == 11)
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Tank Gear!", GOSSIP_SENDER_MAIN, 1);
                if(pPlayer->getClass() == 5 || pPlayer->getClass() == 2 || pPlayer->getClass() == 7 || pPlayer->getClass() == 11)
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Healer Gear!", GOSSIP_SENDER_MAIN, 2);
                if(pPlayer->getClass() != 11 && pPlayer->getClass() != 7)
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Damage Gear!", GOSSIP_SENDER_MAIN, 3);
                else
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Damage Gear!", GOSSIP_SENDER_MAIN, 1002);
                pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());
                break;
            case 1001:
                Options(pPlayer, pCreature, 2, 0, 0);
                break;
            case 1002:
                if(pPlayer->getClass() == 7)
                {
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Enhancer Items!", GOSSIP_SENDER_MAIN, 4);
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Elemental Items!", GOSSIP_SENDER_MAIN, 5);
                pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());
                }
                else if(pPlayer->getClass() == 11)
                {
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Feral Items!", GOSSIP_SENDER_MAIN, 4);
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to start with Moonkin Items!", GOSSIP_SENDER_MAIN, 5);
                pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());
                }
                break;
            case 1:
                Options(pPlayer, pCreature, 1, 1, 0);  //Tank
                break;
            case 2:
                Options(pPlayer, pCreature, 1, 2, 0);  //Healer
                break; 
            case 3:
                Options(pPlayer, pCreature, 1, 3, 0);  //Damagedealer
                break; 
            case 4:
                Options(pPlayer, pCreature, 1, 3, 1);  //Damagedealer Melee (Feral and Enhancer)
                break; 
            case 5:
                Options(pPlayer, pCreature, 1, 3, 2);  //Damagedealer Ranged (Moonkin and Ele)
                break; 
            case 10:
                Options(pPlayer, pCreature, 3, 0, 0);  //Skip DK Start Area
                break; 
            case 11:
                pPlayer->TeleportTo(609, 2355.84f, -5662.2099f, 426.028f, 3.934f, 0);  //Play DK Start Area
				HomeBind(pPlayer, 609, 2355.84f, -5662.2099f, 426.028f, 4298);
                break;                 
            }
            return true;
        }
 
};
 
void AddSC_npc_start_option()
{
    new npc_start_option();
}